package com.axc.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.axc.system.domain.DictinoaryTypeDO;
import com.axc.system.domain.LayUiTree;
import com.axc.system.service.DictinoaryTypeService;
import com.axc.common.utils.R;

/**
 * 
 * 
 * @author admin
 * @email yfq900108@163.COM
 * @date 2018-07-31 13:20:37
 */
 
@Controller
@RequestMapping("/system/dictinoaryType")
public class DictinoaryTypeController {
	@Autowired
	private DictinoaryTypeService dictinoaryTypeService;
	
	@GetMapping()
	@RequiresPermissions("system:dictinoaryType:dictinoaryType")
	String DictinoaryType(){
	    return "system/dictinoaryType/dictinoaryType";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:dictinoaryType:dictinoaryType")
	public List<DictinoaryTypeDO> list() {
		Map<String, Object> query = new HashMap<>(16);
		List<DictinoaryTypeDO> sysDeptList = dictinoaryTypeService.list(query);
		return sysDeptList;
	}
	
	@ResponseBody
	@GetMapping("/tree")
	@RequiresPermissions("system:dictinoaryType:dictinoaryType")
	public LayUiTree[] tree() {
		Map<String, Object> query = new HashMap<>(16);
		List<DictinoaryTypeDO> sysDeptList = dictinoaryTypeService.list(query);
		return GetChildrenByParentId_TreeNodes(sysDeptList, Long.parseLong("0"));
	}

	
    public LayUiTree[] GetChildrenByParentId_TreeNodes(List<DictinoaryTypeDO> sysDeptList, Long parentId)
    {
    	List<LayUiTree> treeList= new ArrayList<LayUiTree>();
    	for(DictinoaryTypeDO item : sysDeptList)
		{
			if( item.getParentId()==parentId)
			{
				LayUiTree tree=new LayUiTree();
				tree.setId(item.getId());
				tree.setname(item.getdictinoaryName());
				tree.setvalue(item.getdictinoaryValue());
				tree.setalias("changyong");
				tree.setparentid(item.getParentId());
				tree.setspread(true);
				LayUiTree[] child = GetChildrenByParentId_TreeNodes(sysDeptList, item.getId());
				tree.setchildren(child);
				treeList.add(tree);
			}
		}
    	LayUiTree[] strings = new LayUiTree[treeList.size()];
        return treeList.toArray(strings);
    }


	
	@GetMapping("/add")
	@RequiresPermissions("system:dictinoaryType:dictinoaryType")
	String add(){
	    return "system/dictinoaryType/add";
	}

	@ResponseBody
	@GetMapping("/get_model_by_id")
	@RequiresPermissions("system:dictinoaryType:dictinoaryType")
	public DictinoaryTypeDO get_model_by_id(Long id){
		DictinoaryTypeDO dictinoaryType = dictinoaryTypeService.get(id);
	    return dictinoaryType;
	}
	
	
	@ResponseBody
	@GetMapping("/get_model_by_query")
	@RequiresPermissions("system:dictinoaryType:dictinoaryType")
	public DictinoaryTypeDO get_model_by_query(DictinoaryTypeDO model){
		
		DictinoaryTypeDO dictinoaryType = dictinoaryTypeService.GetByModel(model);
	    return dictinoaryType;
	}
	
	
	@ResponseBody
	@GetMapping("/get_list_parent")
	@RequiresPermissions("system:dictinoaryType:dictinoaryType")
	public List<DictinoaryTypeDO> get_list_parent(Long parentId) {
		Map<String, Object> query = new HashMap<>(16);
		query.put("parentId", parentId);
		List<DictinoaryTypeDO> sysDeptList = dictinoaryTypeService.list(query);
		return sysDeptList;
	}
	
	@GetMapping("/edit")
	@RequiresPermissions("system:dictinoaryType:dictinoaryType")
	String edit(){
	    return "system/dictinoaryType/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:dictinoaryType:dictinoaryType")
	public R save( DictinoaryTypeDO model){
		
		DictinoaryTypeDO model1= get_model_by_query(model);
		if(model1==null)
		{
			if(dictinoaryTypeService.save(model)>0){
				return R.ok();
			}
			else {

				return R.error(2,"更新失败");
			}
		}
		else
		{
			return R.error(2,"字典类型编码为："+model.getdictinoaryValue()+";<br/>字典类型名称为："+model.getdictinoaryName()+"<br/>的记录已存在！");
		}
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:dictinoaryType:dictinoaryType")
	public R update( DictinoaryTypeDO model){
		
		DictinoaryTypeDO model1= get_model_by_query(model);
		if(model1==null||model1.getId()==model.getId())
		{
			int count=dictinoaryTypeService.update(model);
			if(count==1)
			{
				return R.ok();
			}
			else {

				return R.error(2,"更新失败");
			}
		}
		else
		{
			return R.error(2,"字典类型编码为："+model.getdictinoaryValue()+";<br/>字典类型名称为："+model.getdictinoaryName()+"<br/>的记录已存在！");
		}
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:dictinoaryType:dictinoaryType")
	public R remove( Long id){
		
		Map<String, Object> query = new HashMap<>(16);
		query.put("parentId", id);
		List<DictinoaryTypeDO> sysDeptList = dictinoaryTypeService.list(query);
		if(sysDeptList.size()==0)
		{
			if(dictinoaryTypeService.remove(id)>0){
				return R.ok();
				}
		}
		else {
			return R.error(2,"该节点存在子节点，不能删除！");
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:dictinoaryType:dictinoaryType")
	public R remove(@RequestParam("ids[]") Long[] ids){
		dictinoaryTypeService.batchRemove(ids);
		return R.ok();
	}
	
	
	
	
}
